﻿/*
  Project:       
  File:          CopyRegisterHandler.ashx
  Description:   
 *               
 *               
 *               
  Author:        Lanny
  Date:          Aug,2014
  version:       2.0
  Modify:
 
                 
 Comments:
 * 1. 2014.07.17 增加一些异常操作。

 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Data;
namespace TaoBaoProject.Handler.RegisterHandler
{
  /// <summary>
  /// Summary description for CopyRegisterHandler
  /// </summary>
  public class CopyRegisterHandler : IHttpHandler
  {
    protected static readonly log4net.ILog log =
   log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    QQRelationBLL qqBLL = new QQRelationBLL();
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string wangwang = context.Request.Params["uWangwang"].ToString();
      string shopname = context.Request.Params["uShopName"].ToString();
      string QQ = context.Request.Params["qq"];
      string pid = context.Request.Params["pid"];
      try
      {
        int ProId = Convert.ToInt32(context.Request.Params["ProId"]);

        if (shopname.Contains(','))
          shopname = shopname.Remove(shopname.Length - 1);

        if (QQ.Contains(','))
          QQ = QQ.Remove(QQ.Length - 1);
        RegisterBLL registerBll = new RegisterBLL();
        List<string> allQQs = RegisterBLL.GetAllRelatesQQByQQ(QQ);

        string basicStr = @"select ProjectFileds.*,Shops.ShopName,Shops.ShopColor from ProjectFileds inner join Shops on ProjectFileds.ShopID = Shops.ShopID where ";
        string qqRelate, lastStr;
        DataTable dataRelateQQ;
        DateTime latestDate;

        qqRelate = ""; lastStr = ""; latestDate = DateTime.Now;
        if (allQQs.Count == 0)
        {
          qqRelate += @" ProjectFileds.QQ = N'" + QQ + "'";
        }
        else
        {
          foreach (string qq in allQQs)
            qqRelate += @" ProjectFileds.QQ = N'" + qq + "' or ";
          qqRelate = qqRelate.Remove(qqRelate.LastIndexOf("or"));
        }

        lastStr = basicStr + qqRelate + " order by Date DESC";
        dataRelateQQ = RegisterBLL.GetRegisterDataBySql(lastStr);
        if (dataRelateQQ.Rows.Count > 0)
        {
          latestDate = Convert.ToDateTime(dataRelateQQ.Rows[0]["Date"]);
        }
        string userName = context.Request.Params["u"].ToString();

        RegisterBLL.CopyRegister(Convert.ToInt32(pid), userName);
        context.Response.Write("{success:'1'}");

        return;
      }
      catch (Exception ex)
      {
        //errorMsg
        context.Response.Write("{exist:'1',errorMsg:'" + ex.Message + "'}");
        log.Error("登记[" + wangwang + "]的时候发生了异常，异常信息是：" + ex.Message);
      }

    }
    public int GetDays(DateTime d1, DateTime d2)
    {

      int d = (d2 - d1).Days;
      return d;
    }
    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}